package com.wangzeqi.changliaoba.messege;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.wangzeqi.changliaoba.R;
import com.wangzeqi.custom.CircleImageView;

import java.util.ArrayList;

/**
 * Created by dllo on 16/3/5.
 */
public class MessegeListViewAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<SingleData> datas;

    public MessegeListViewAdapter(Context context, ArrayList<SingleData> datas) {
        this.context = context;
        this.datas = datas;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder ;
        if(convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.item_messege_listview,null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.textViewName.setText(datas.get(position).getName());
        holder.textViewContent.setText(datas.get(position).getContent());
        holder.textViewTime.setText(datas.get(position).getTime());
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, ChatActivity.class);
                intent.putExtra("username", holder.textViewName.getText().toString());
                context.startActivity(intent);
            }
        });
        return convertView;
    }

    class ViewHolder {
        private CircleImageView circleImageView;
        private TextView textViewName;
        private TextView textViewContent;
        private TextView textViewTime;

        public ViewHolder(View view) {
            circleImageView = (CircleImageView) view.findViewById(R.id.messege_listview_iv);
            textViewName = (TextView) view.findViewById(R.id.messege_listview_tv_name);
            textViewContent = (TextView) view.findViewById(R.id.messege_listview_tv_content);
            textViewTime = (TextView) view.findViewById(R.id.messege_listview_tv_time);
        }
    }
}
